<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<html lang="en">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<meta name="generator" content="PHPDoctor 2RC4 (http://peej.github.com/phpdoctor/)">
<meta name="when" content="Mon, 02 May 2011 02:46:56 +0000">

<link rel="stylesheet" type="text/css" href="../stylesheet.css">
<link rel="start" href="../overview-summary.html">

<title>utils.php (Cornice)</title>

</head>
<body id="file" onload="parent.document.title=document.title;">

<div class="header">
<h1>Cornice</h1>
<ul>
<li><a href="../overview-summary.html">Overview</a></li>
<li>Package</li><li>Class</li><li>Tree</li><li><a href="../overview-files.html">Files</a></li>
<li><a href="../deprecated-list.html">Deprecated</a></li>
<li><a href="../todo-list.html">Todo</a></li>
<li><a href="../index-all.html">Index</a></li>
</ul>
</div>

<div class="small_links">
<a href="../index.html" target="_top">Frames</a>
<a href="../source\utils.php.html" target="_top">No frames</a>
</div>
<hr>

<h1>utils.php</h1>
<div class="comment" id="overview_description"><p>============================================================================
Cornice PHP Framework & Website Builder
Freeware (f) 2011 by Inforo Software & Computer
============================================================================</p></div>

<hr>

<a name="line1"></a><pre><?php
<a name="line2"></a>/**
<a name="line3"></a> *============================================================================
<a name="line4"></a> * Cornice PHP Framework & Website Builder
<a name="line5"></a> * Freeware (f) 2011 by Inforo Software & Computer
<a name="line6"></a> *============================================================================
<a name="line7"></a> */
<a name="line8"></a> 
<a name="line9"></a>/**
<a name="line10"></a> * Class contains miscellaneous utils
<a name="line11"></a> * @author Nicola Dinh
<a name="line12"></a> * @version 2011.04.2217
<a name="line13"></a> */
<a name="line14"></a>class utils {
<a name="line15"></a>
<a name="line16"></a>  /**
<a name="line17"></a>   * Default instance level constructor
<a name="line18"></a>   * Just decorative, should not be called due to having only static methods
<a name="line19"></a>   */
<a name="line20"></a>  public function __construct() {
<a name="line21"></a>    //
<a name="line22"></a>  }
<a name="line23"></a>  
<a name="line24"></a>  /**
<a name="line25"></a>   * Trim trailing & tailing characters, also extra spaces in between
<a name="line26"></a>   * @param string $text The text to trim
<a name="line27"></a>   * @return string The trimmed text
<a name="line28"></a>   */
<a name="line29"></a>  public static function trim_neat($text) {
<a name="line30"></a>    $text = trim($text);
<a name="line31"></a>    while (strpos($text,chr(32).chr(32))!==false)
<a name="line32"></a>      $text = str_replace(chr(32).chr(32),chr(32),$text);
<a name="line33"></a>    return $text;
<a name="line34"></a>  }
<a name="line35"></a>  
<a name="line36"></a>  /**
<a name="line37"></a>   * Convert object into multi-level array
<a name="line38"></a>   * @param object $object The object to convert
<a name="line39"></a>   * @return array The corresponding multi-level array to the object   
<a name="line40"></a>   */
<a name="line41"></a>  public static function obj_to_arr($object) {
<a name="line42"></a>    if (!is_object($object) && !is_array($object))
<a name="line43"></a>      return $object;
<a name="line44"></a>  
<a name="line45"></a>    //fun stuff
<a name="line46"></a>    if (is_object($object))
<a name="line47"></a>      $properties = get_object_vars($object);
<a name="line48"></a>    else
<a name="line49"></a>      $properties = $object;
<a name="line50"></a>    
<a name="line51"></a>    //convert
<a name="line52"></a>    $array = array();
<a name="line53"></a>    foreach ($properties as $name=>$value) 
<a name="line54"></a>      if (is_object($value) || is_array($value))
<a name="line55"></a>        $array[$name] = self::obj_to_arr($value);
<a name="line56"></a>      else
<a name="line57"></a>        $array[$name] = $value; 
<a name="line58"></a>        
<a name="line59"></a>    return $array;
<a name="line60"></a>  }//obj_to_arr
<a name="line61"></a>  
<a name="line62"></a>  /**
<a name="line63"></a>   * Get value from a certain path in multi-level array
<a name="line64"></a>   * @param array $array The multi-level array
<a name="line65"></a>   * @param string $arr_path The path in array, can not be blank
<a name="line66"></a>   * @return object The value of that array level (object or primitive)
<a name="line67"></a>   */
<a name="line68"></a>  public static function get_arr_value($array,$arr_path) {
<a name="line69"></a>    $arr_path = str_replace(chr(32),"",$arr_path);
<a name="line70"></a>    $tokens = explode("/",$arr_path);
<a name="line71"></a>    
<a name="line72"></a>    //find value
<a name="line73"></a>    $value = $array;
<a name="line74"></a>    foreach ($tokens as $token) 
<a name="line75"></a>      $value = $value[$token];
<a name="line76"></a>    
<a name="line77"></a>    //return
<a name="line78"></a>    return $value;
<a name="line79"></a>  }  
<a name="line80"></a>  
<a name="line81"></a>  /**
<a name="line82"></a>   * Get file extension 
<a name="line83"></a>   * @param string $file_path The path to a file
<a name="line84"></a>   * @return string The file extension in lowercase
<a name="line85"></a>   */
<a name="line86"></a>  public static function get_file_ext($file_path) {
<a name="line87"></a>    $parts = pathinfo($file_path);
<a name="line88"></a>    return $parts["extension"];
<a name="line89"></a>  }
<a name="line90"></a>  
<a name="line91"></a>  /**
<a name="line92"></a>   * Get file names in a directory
<a name="line93"></a>   * @param string $dir_path The path to directory
<a name="line94"></a>   * @return array The list of files (at least 2, '.' & '..')
<a name="line95"></a>   */
<a name="line96"></a>  public static function get_files_in($dir_path,$extension=null) {
<a name="line97"></a>    if (!file_exists($dir_path))
<a name="line98"></a>      return array();
<a name="line99"></a>    if ($extension!=null)
<a name="line100"></a>      $extension = strtolower($extension);
<a name="line101"></a>  
<a name="line102"></a>    $dir_handle = opendir($dir_path);
<a name="line103"></a>    if ($dir_handle===false)
<a name="line104"></a>      return array();
<a name="line105"></a>      
<a name="line106"></a>    $files = array();
<a name="line107"></a>    while (($file=readdir($dir_handle))!==false) {
<a name="line108"></a>      $file = strtolower($file);
<a name="line109"></a>      if ($extension==null)
<a name="line110"></a>        array_push($files,$file);    
<a name="line111"></a>      else
<a name="line112"></a>      if (self::get_file_ext($file)==$extension)
<a name="line113"></a>        array_push($files,$file);
<a name="line114"></a>    }
<a name="line115"></a>      
<a name="line116"></a>    closedir($dir_handle);
<a name="line117"></a>    return $files;
<a name="line118"></a>  }
<a name="line119"></a>}</pre>
<div class="header">
<h1>Cornice</h1>
<ul>
<li><a href="../overview-summary.html">Overview</a></li>
<li>Package</li><li>Class</li><li>Tree</li><li><a href="../overview-files.html">Files</a></li>
<li><a href="../deprecated-list.html">Deprecated</a></li>
<li><a href="../todo-list.html">Todo</a></li>
<li><a href="../index-all.html">Index</a></li>
</ul>
</div>

<div class="small_links">
<a href="../index.html" target="_top">Frames</a>
<a href="../source\utils.php.html" target="_top">No frames</a>
</div>
<hr>

<p id="footer">This document was generated by <a href="http://peej.github.com/phpdoctor/">PHPDoctor: The PHP Documentation Creator</a></p>

</body>

</html>